package com.cnhis.cloudhealth.clinical.hosnurse.hosparams.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StringUtils;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosparams.dao.HosParamsDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
@Service
public class HosParamsBo {
	@Resource
    private HosParamsDao hosParamsDao;
	@Resource
	private GetNewId getNewId;

	private ModelVo packModelVo(String nid,Map map){
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			list = hosParamsDao.queryForList(nid,map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
		}
		if(list!=null&&list.size()>0) {
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
			mo.setRows(list);
		}
		return mo;
	}
	private boolean checkExistsParameters(Map map)throws Exception{
		return hosParamsDao.checkExistsParameters(map)>0;
	}

	public String getSysValue(int productId,int programId,int paramNo )throws Exception{
		return hosParamsDao.getSysValue(productId,programId,paramNo);
	}
	public Boolean savedOneUserParameters(Map map)throws Exception{
		Boolean result = false;
		Integer cnt1 = (Integer)hosParamsDao.queryForExtObject("HosParams.soup_001",map);	//判断是否存在此系统参数
		Integer privilege = MapUtils.getInteger(map,"privilege");
		map.put("pid", getNewId.nextId());
		map.put("uid", getNewId.nextId());
		if (cnt1==0){
			hosParamsDao.insert("HosParams.soup_002",map);		//插入系统参数
			if(privilege==1||privilege==2||privilege==3){
				hosParamsDao.insert("HosParams.soup_003",map);	//插入用户参数
			}
			result = true;
		}else{
			String pvalue = MapUtils.getString(map,"pvalue");
			if (privilege == 0) {
				String psvalue = (String)hosParamsDao.queryForExtObject("HosParams.getSysParamValue",map);
				if(psvalue==null||!StringUtils.isEquals(psvalue,pvalue)) {	//如果和原来参数一致无需更新
					Integer cnt4 = (Integer) hosParamsDao.queryForExtObject("HosParams.soup_004", map); //根据权限类型判断是否存在此系统参数
					if (cnt4 > 0 && privilege == 0) {
						hosParamsDao.delete("HosParams.soup_005", map);  //删除相关用户参数
					}
					Map<String, Object> pmap1 = (Map<String, Object>) hosParamsDao.queryForExtObject("HosParams.soup_007", map); //更改操作参数1
					if (pmap1 != null) {
						map.put("empname", MapUtils.getString(pmap1, "empname"));
						map.put("host", MapUtils.getString(pmap1, "host"));
						map.put("ip", MapUtils.getString(pmap1, "ip"));
					}
					Map<String, Object> pmap2 = (Map<String, Object>) hosParamsDao.queryForExtObject("HosParams.soup_008", map); //更改操作参数2
					if (pmap2 != null) {
						map.put("programname", MapUtils.getString(pmap2, "programname"));
						map.put("event", MapUtils.getString(pmap2, "event"));
					}
					long eventlogid = getNewId.nextId();
					map.put("eventlogid", eventlogid);
					map.put("messageid", 0);
					hosParamsDao.insert("HosParams.soup_009", map);    //插入更改参数日志记录
					hosParamsDao.update("HosParams.soup_010", map);    //更新系统参数
					result = true;
				}
			}else if (privilege == 1 || privilege == 2 || privilege == 3) {
				String uvalue = (String)hosParamsDao.queryForExtObject("HosParams.getUserParamValue",map);
				if(uvalue==null||!StringUtils.isEquals(uvalue,pvalue)) {    //如果和原来参数一致则无需更新
					hosParamsDao.delete("HosParams.soup_006", map);    //根据用户id删除用户参数
					Integer cnt10 = (Integer) hosParamsDao.queryForExtObject("HosParams.soup_011", map);
					if (cnt10 == 0) {
						hosParamsDao.update("HosParams.soup_010", map);    //更新系统参数
						result = true;
					} else {
						hosParamsDao.insert("HosParams.soup_012", map);    //插入更改参数日志记录
						result = true;
					}
				}
			}
		}
		return result;
	}
	public ModelVo selectPublicParams(){
		return packModelVo("HosParams.getPublicParams",null);
	}
	public ModelVo selectBedParams(){
		return packModelVo("HosParams.4176",null);
	}
	//Clinic_BEO1_Insert 自动记账项目设置插入
	public ModelVo insertBeo(Map map)throws Exception{
		String lxml = MapUtils.getString(map,"lxml"); //获取相关json信息字符串
		Integer asign = MapUtils.getInteger(map,"asign",0);
		if(asign==2){
			hosParamsDao.delete("HosParams.auto_fee_delete", map);
			return new ModelVo(StaticKeys.OH_SUCCESS, "自动记账项目数据保存成功！");
		}
		if(StringUtils.isBlank(lxml)){
			return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，自动记账项目相关信息为空！");
		}
		JSONObject job = JSONObject.fromObject(lxml);
		List<Map<String,Object>> list = JsonUtil.parserToList(job.getJSONArray("beo1"));
		for(Map<String,Object> mo:list){
			mo.put("beo01",getNewId.nextId());
		}
//		if()
		map.put("beoList",list);
		
		if(list!=null&&list.size()>0){
			if(asign==0) {
				hosParamsDao.delete("HosParams.auto_fee_001", map);    //删除相关项目
				hosParamsDao.insert("HosParams.auto_fee_002", map);    //重新插入自动记账相关项目
			}else if(asign==1){
				for(int i=0;i<list.size();i++){
					map.put("bck01", list.get(i).get("bck01"));
					hosParamsDao.delete("HosParams.auto_fee_003", map);
				}
				    //删除相关项目
				hosParamsDao.insert("HosParams.auto_fee_002", map);    //重新插入自动记账相关项目
			}
		}
		return new ModelVo(StaticKeys.OH_SUCCESS, "自动记账项目数据保存成功！");
	}
	//病区床位和自动护理项目设置查询
	public ModelVo queryAutoChargeSetting()throws Exception{
		  ModelVo mo = new ModelVo();
			List list =null;
			try {
				   list=hosParamsDao.queryAutoChargeSetting();
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
		
	}
	//病人类别自动记账项目设置查询
	public ModelVo selectPtypeautofee(){
		return packModelVo("HosParams.7741",null);
	}
	public ModelVo selectFeetypetree(){
		return packModelVo("HosParams.1771",null);
	}
	public ModelVo selectFeetypedetail(Map map){
		return packModelVo("HosParams.1061",map);
	}
	public ModelVo select2008(Map map){
		return packModelVo("HosParams.2008",map);
	}
	public ModelVo select2009(Map map){
		return packModelVo("HosParams.2009",map);
	}
	public ModelVo queryAuto2040(){
		return packModelVo("HosParams.2040",null);
	}
	public ModelVo queryAuto2011(Map map){
		return packModelVo("HosParams.2011",map);
	}
}
